﻿Imports BUS
Imports DTO

Public Class MH_Quanlydocgia

    'Dat lai mau cua cac control
    Private Sub Reset_Backcolor()
        TH_Hoten.BackColor = Color.White
        TH_Cmnd.BackColor = Color.White
        TH_Diachi.BackColor = Color.White
        TH_Dienthoai.BackColor = Color.White
        TH_Gioitinh.BackColor = Color.White
        TH_Lydohuythe.BackColor = Color.White
    End Sub

    'Dat lai trang thai va text cac control
    Private Sub Reset_Content()
        TH_Mathe.Text = ""
        TH_Hoten.Text = ""
        TH_Cmnd.Text = ""
        TH_Diachi.Text = ""
        TH_Dienthoai.Text = ""
        TH_Gioitinh.Text = "Nam"
        btn_Capnhat.Enabled = False
        btn_Huy.Enabled = False
        btn_Themdg.Enabled = True
        btn_Huythe.Enabled = False
        TH_Ngayhuy.Enabled = False
        TH_Lydohuythe.Enabled = False
        TH_Ngaylapthe.Enabled = True
    End Sub

    'Kiem tra chuoi nhap co phai la so khong
    Private Function Kiemtra_nhapint(ByVal s As String) As String
        Dim Kq As String = ""
        Dim Hop_le As Boolean
        Dim t As Integer

        Hop_le = Integer.TryParse(s, t)
        If Not Hop_le Then
            Kq &= "Cần nhập số" & vbCrLf
        End If

        Return Kq
    End Function

    'Kiem tra thong tin doc gia co hop le khong
    Private Function Kiemtra_ThongtinDocgia() As String
        Dim Kq As String = ""
        Dim Hop_le As Boolean
        Dim t As Integer

        Reset_Backcolor()
        'CMND
        Hop_le = Integer.TryParse(TH_Cmnd.Text, t)
        If Not Hop_le Then
            TH_Cmnd.BackColor = Color.Red
            Kq = "Fail"
        End If
        'Dien thoai
        Hop_le = Integer.TryParse(TH_Dienthoai.Text, t)
        If Not Hop_le Then
            TH_Dienthoai.BackColor = Color.Red
            Kq = "Fail"
        End If
        'Dia chi
        If TH_Diachi.Text = "" Then
            TH_Diachi.BackColor = Color.Red
            Kq = "Fail"
        End If
        'Ho ten
        If TH_Hoten.Text = "" Then
            TH_Hoten.BackColor = Color.Red
            Kq = "Fail"
        End If
        'Gioi tinh
        If TH_Gioitinh.Text = "" Then
            TH_Gioitinh.BackColor = Color.Red
            Kq = "Fail"
        End If

        Return Kq
    End Function

    'Load
    Private Sub MH_Quanlydocgia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TH_Chontieuchitimdocgia.SelectedIndex = 0
    End Sub

    'Tim doc gia
    Private Sub btn_TimDocgia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_TimDocgia.Click
        TH_Trangthaitracuudocgia.Text = "Đang tra cứu ..."
        TH_Trangthaitracuudocgia.Update()

        Dim output = "OK"

        If TH_Tieuchitimdocgia.Text = "" Then
            TH_DS_Docgia.DataSource = DocGiaBus.LayBang_Tracuu()
        Else
            Select Case TH_Chontieuchitimdocgia.SelectedIndex
                Case 0
                    output = Kiemtra_nhapint(TH_Tieuchitimdocgia.Text)
                    If output = "" Then
                        TH_DS_Docgia.DataSource = DocGiaBus.LayBang_Tracuu_Theomathe(TH_Tieuchitimdocgia.Text)
                    End If
                Case 1
                    TH_DS_Docgia.DataSource = DocGiaBus.LayBang_Tracuu_Theohoten(TH_Tieuchitimdocgia.Text)
                Case 2
                    TH_DS_Docgia.DataSource = DocGiaBus.LayBang_Tracuu_Theocmnd(TH_Tieuchitimdocgia.Text)
            End Select
        End If

        TH_Trangthaitracuudocgia.Text = output
    End Sub

    'Lay thong tin cu the cua doc gia tu danh sach
    Private Sub TH_DS_Docgia_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles TH_DS_Docgia.CellClick
        Dim mt = TH_DS_Docgia.CurrentRow.Cells("Ma_the").Value
        If mt > 0 Then
            'Init display
            Reset_Backcolor()
            btn_Capnhat.Enabled = True
            btn_Huy.Enabled = True
            btn_Themdg.Enabled = False
            TH_Ngaylapthe.Enabled = False

            Dim the = TheThuVienBus.LayThe(mt)
            Dim docgia = DocGiaBus.LayDocGia(the.MDG)
            TH_Mathe.Text = the.MT
            TH_Hoten.Text = docgia.Ho_ten
            TH_Cmnd.Text = docgia.Cmnd
            TH_Diachi.Text = docgia.Dia_chi
            TH_Dienthoai.Text = docgia.Dien_thoai
            TH_Gioitinh.Text = docgia.Gioi_tinh
            TH_Ngaylapthe.Value = the.Ngay_lap_the
        End If
    End Sub

    'Cap nhat thong tin doc gia
    Private Sub btn_Capnhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Capnhat.Click
        Dim chuoiloi = Kiemtra_ThongtinDocgia()
        'Cap nhat
        If chuoiloi = "" Then
            Dim docgia As New DocGia
            Dim the As TheThuVien = TheThuVienBus.LayThe(TH_Mathe.Text)
            docgia.MDG = the.MDG
            docgia.Ho_ten = TH_Hoten.Text
            docgia.Cmnd = TH_Cmnd.Text
            docgia.Dia_chi = TH_Diachi.Text
            docgia.Dien_thoai = TH_Dienthoai.Text
            docgia.Gioi_tinh = TH_Gioitinh.Text
            DocGiaBus.CapNhat(docgia)
            'Reset display
            btn_TimDocgia.PerformClick()
            Reset_Backcolor()
            Reset_Content()
        End If
    End Sub

    'Bat che do xoa doc gia (huy the)
    Private Sub btn_Huy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Huy.Click
        btn_Huythe.Enabled = True
        TH_Ngayhuy.Enabled = True
        TH_Lydohuythe.Enabled = True
    End Sub

    'Tien hanh huy the
    Private Sub TH_Huythe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Huythe.Click
        If TH_Lydohuythe.Text = "" Then
            TH_Lydohuythe.BackColor = Color.Red
        Else
            'Huy the
            Dim thehuy As New TheHuy
            Dim ttv As TheThuVien = TheThuVienBus.LayThe(TH_Mathe.Text)
            thehuy.MT = ttv.MT
            thehuy.Ngay_huy = TH_Ngayhuy.Value
            thehuy.Ly_do = TH_Lydohuythe.Text
            TheHuyBus.Them(thehuy)
            TheThuVienBus.Xoa(ttv.MT)
            'Reset display
            btn_TimDocgia.PerformClick()
            Reset_Backcolor()
            Reset_Content()
        End If
    End Sub

    'Lap the thu vien moi
    Private Sub btn_Themdg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Themdg.Click
        Dim chuoiloi = Kiemtra_ThongtinDocgia()
        If chuoiloi = "" Then
            Dim the As New TheThuVien
            Dim docgia As New DocGia
            'Them doc gia
            the.Ngay_lap_the = TH_Ngaylapthe.Value
            the.Ngay_het_han = TH_Ngaylapthe.Value.AddYears(1)
            docgia.Ho_ten = TH_Hoten.Text
            docgia.Cmnd = TH_Cmnd.Text
            docgia.Dia_chi = TH_Diachi.Text
            docgia.Dien_thoai = TH_Dienthoai.Text
            docgia.Gioi_tinh = TH_Gioitinh.Text
            DocGiaBus.ThemDocGia(docgia, the)
            'Reset display
            btn_TimDocgia.PerformClick()
            Reset_Backcolor()
            Reset_Content()
        End If
    End Sub

    'Reset
    Private Sub btn_Reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Reset.Click
        Reset_Backcolor()
        Reset_Content()
    End Sub

    Private Sub TH_Tieuchitimdocgia_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TH_Tieuchitimdocgia.KeyDown
        If e.KeyValue = Keys.Enter Then
            btn_TimDocgia.PerformClick()
        End If
    End Sub
End Class